import Cesium from '../Ces/Cesium'
import CommonPickHandler from './CommonPickHandler'
import EditableHandler from './EditableHandler'
import FlyHandler from './FlyHandler'
import EditablePoint from './EditablePoint'
import DrawHandler from '../Geometry/DrawHandler'
import InternalHandler from './InternalHandler'
import PositionHandler from './PositionHandler'
import WeQuery from '../Display/WeQuery'

class HandlerManager {
  private WE
  private _currentHandler
  private _editHander
  private _flyHander: FlyHandler
  private _internalHandler
  constructor(WE) {
    this.WE = WE
  }

  get currentHander() {
    return this._currentHandler
  }
  get currentHandler() {
    return this._currentHandler
  }
  get internalHandler() {
    if (!Cesium.defined(this._internalHandler)) {
      this._internalHandler = new InternalHandler(this.WE.viewer)
    }

    return this._internalHandler
  }

  get flyHander() {
    return this._flyHander
  }

  set enableFlyHandler(value) {
    if (value) {
      if (!Cesium.defined(this._flyHander)) {
        this._flyHander = new FlyHandler(this.WE)
      }
    } else {
      this._flyHander = this._flyHander && this._flyHander.destroy()
    }
  }

  get enableFlyHandler() {
    return Cesium.defined(this._flyHander)
  }

  createEditablePoint() {
    return new EditablePoint()
  }

  startDraw(options) {
    this.stop()

    const type = options.type?? 'line'

    let promise

    if (type === 'line' || type === 'rectangle' || type === 'polyline' || type === 'polygon') {
      const drawHandler = new DrawHandler(Object.assign(options, {
        WE: this.WE,
      }))

      drawHandler.startDraw()
      promise = drawHandler.readyPromise
      this._currentHandler = drawHandler
    }

    return promise
  }

  startPickPosition() {
    this.stop()
    this._currentHandler = new PositionHandler(this.WE)
    this._currentHandler.start()
  }

  startPickWeImagery() {
    this.stop()
    const { WE } = this

    this._currentHandler = new PositionHandler(WE)
    this._currentHandler.showToolTip = false
    this._currentHandler.query = new WeQuery(WE)
    this._currentHandler.start()
  }

  startPick(options: any = {}) {
    this.stop()
    const type = options.handleType?? 'CommonPick'

    if (type === 'CommonPick') {
      this._currentHandler = new CommonPickHandler(this.WE.viewer, options)
      this._currentHandler.start()
      return this._currentHandler
    }
  }

  startEdit() {
    this.stopEdit()
    this._editHander = new EditableHandler(this.WE)
    this._editHander.start()
  }

  stopEdit() {
    this._editHander = this._editHander && this._editHander.destroy()
  }

  stop() {
    this._currentHandler = this._currentHandler && this._currentHandler.destroy()
  }

  destroy() {
    this.stop()
    this._internalHandler = this._internalHandler && this._internalHandler.destroy()
  }

  isDestroyed() {
    return false
  }

  update(frameState) {
    if (this._currentHandler && this._currentHandler.update) {
      this._currentHandler.update(frameState)
    }
  }
}




export default HandlerManager
